package demo.google;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.type.AviatorFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;

/**
 * AviatorScript 是一门高性能、轻量级寄宿于 JVM 之上的脚本语言。
 * <h1>特性介绍</h1>
 * <ol>
 * <li>支持数字、字符串、正则表达式、布尔值、正则表达式等基本类型，完整支持所有 Java 运算符及优先级等。
 * <li>函数是一等公民，支持闭包和函数式编程。
 * <li>内置 bigint/decmal 类型用于大整数和高精度运算，支持运算符重载得以让这些类型使用普通的算术运算符 (加减乘除) 参与运算。
 * <li>完整的脚本语法支持，包括多行数据、条件语句、循环语句、词法作用域和异常处理等。
 * <li>函数式编程结合 Sequence 抽象，便捷处理任何集合。
 * <li>轻量化的模块系统。
 * <li>多种方式，方便地调用 Java 方法，完整支持 Java 脚本 API（方便从 Java 调用脚本）。
 * <li>丰富的定制选项，可作为安全的语言沙箱和全功能语言使用。
 * <li>轻量化，高性能，通过直接将脚本翻译成 JVM 字节码，AviatorScript 的基础性能较好。
 * </ol>
 * 
 * 使用场景包括：
 * 
 * <li>规则判断及规则引擎
 * <li>公式计算
 * <li>动态脚本控制
 * <li>集合数据 ELT 等 ……
 * 
 * @author https://github.com/killme2008/aviatorscript
 *
 */
public class AviatorScriptDemo {

    /**
     * <li>支持绝大多数运算操作符，包括算术操作符、关系运算符、逻辑操作符、位运算符、正则匹配操作符（=~）、三元表达式
     * <li>支持传入变量
     */
    @Test
    public void test1() {
        Map<String, Object> env = new HashMap<>();
        env.put("a", 1);
        env.put("b", 2);
        String expression;
        {
            expression = "a+b";
            Object result = AviatorEvaluator.execute(expression, env);
            System.out.println(expression + " = " + result);
        }
        {
            expression = "a == b";
            Object result = AviatorEvaluator.execute(expression, env);
            System.out.println(expression + " = " + result);
        }
        {
            expression = "a == b ? '相等' : '不相等'";
            Object result = AviatorEvaluator.execute(expression, env);
            System.out.println(expression + " = " + result);
        }
        {
            expression = " a > 1 && b >2";
            Object result = AviatorEvaluator.execute(expression, env);
            System.out.println(expression + " = " + result);
        }
        {
            expression = "a / (a+b)";
            Object result = AviatorEvaluator.execute(expression, env);
            System.out.println(expression + " = " + result);
        }

    }

    /**
     * 常用函数库
     */
    @Test
    public void test2() {
        String expression;
        {
            expression = "string.length('hello aviator !')";
            Object result = AviatorEvaluator.execute(expression);
            System.out.println(expression + " = " + result);
        }

        {
            expression = "sysdate()";
            Object result = AviatorEvaluator.execute(expression);
            System.out.println(expression + " = " + result);
        }

        {
            expression = "rand()";
            Object result = AviatorEvaluator.execute(expression);
            System.out.println(expression + " = " + result);
        }

    }

    /**
     * <h1>支持自定义函数</h1>如果自带函数不能满足开发需要，
     * aviator还支持自定义函数，只要实现com.googlecode.aviator.runtime.type.AviatorFunction接口,
     * 并注册到AviatorEvaluator即可使用！
     * <p>
     * 注册函数通过AviatorEvaluator.addFunction方法, 移除可以通过removeFunction方法
     */
    @Test
    public void test3() {
        A a = new A();
        AviatorEvaluator.addFunction(a);
    }

    /**
     * 支持自定义函数
     * 
     * @author junying.han
     *
     */
    class A implements AviatorFunction {

        @Override
        public AviatorObject call() throws Exception {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }

        @Override
        public String getName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12,
                AviatorObject arg13) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14, AviatorObject arg15) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14, AviatorObject arg15, AviatorObject arg16) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14, AviatorObject arg15, AviatorObject arg16, AviatorObject arg17) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14, AviatorObject arg15, AviatorObject arg16, AviatorObject arg17,
                AviatorObject arg18) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14, AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18,
                AviatorObject arg19) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14, AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18,
                AviatorObject arg19, AviatorObject arg20) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
                AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, AviatorObject arg7, AviatorObject arg8,
                AviatorObject arg9, AviatorObject arg10, AviatorObject arg11, AviatorObject arg12, AviatorObject arg13,
                AviatorObject arg14, AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18,
                AviatorObject arg19, AviatorObject arg20, AviatorObject... args) {
            // TODO Auto-generated method stub
            return null;
        }
    }

    /**
     * 支持赋值
     */
    @Test
    public void test4() {
        String expression;
        {
            expression = "a = 10; a + 20";
            Object result = AviatorEvaluator.execute(expression);
            System.out.println(expression + " = " + result);
        }

    }

}
